# -*- coding: utf-8 -*-
# -文件说明：excel相关操作-
# @Time    : 2019/9/9 15:24
# @Author  : hwx
# @Email   : @163.com
# @File    : exceldemo.py
# @Software: PyCharm
import xlrd
import xlwt
import re
from xlutils.copy import copy


def read_file():
    with open('textfiles/data1.txt', 'r', encoding='UTF-8') as f1:  # 注意编码
        readdata = f1.readline()
        print(readdata)


def write_excelfile2():
    str1 = '第二行，第三列内容'
    str2 = '第三行，第三列内容'
    str3 = '第四行，第三列内容'
    str4 = '第五行，第三列内容'
    str5 = '第二行，第四列内容'
    str6 = '第二行，第五列内容'
    str7 = '第二行，第六列内容'

    w2 = xlrd.open_workbook('testdemo3.xls')
    #sheet2 = w2.sheet_by_name('xlwtishere')
    # #xlutils:修改excel # 拷贝一份原来的excel
    wb2 = copy(w2)
    sheet2 = wb2.get_sheet(0)
    sheet2.write(1, 2, str1)
    sheet2.write(2, 2, str2)
    sheet2.write(3, 2, str3)
    sheet2.write(4, 2, str4)
    sheet2.write(1, 5, '')
    sheet2.write(1, 3, str5)
    sheet2.write(1, 4, str6)
    wb2.save('testdemo3.xls')


def write_excelfile3():
    w3 = xlwt.Workbook()
    sheet1 = w3.add_sheet('xlwtishere')
    w3.save('testdemo3.xls')


def write_excelfile():
    w2 = xlrd.open_workbook('testdemo3.xls')
    # #xlutils:修改excel # 拷贝一份原来的excel
    wb2 = copy(w2)
    ws = wb2.get_sheet(0)
    row = 0
    li = 1
    i = 0
    strtemp = ''
    with open('textfiles/2015下.txt', 'r', encoding='UTF-8') as f1:  # 注意编码
        while True:
            line = f1.readline()
            print(line)

            match = re.match(r'[1-9]+..*', line)
            if match:
                print('***********************************************')
                print('match不为空：', match is None)
                print(match.group())
                if strtemp != '':
                    ws.write(li + i, row + 1, strtemp)
                    strtemp = ''
                    i = i + 1
                ws.write(li + i, row, match.group())


            else:
                print('**********+++++++++++++++++++++++++++++++++++++******')
                print('match为空：', match is not None)
                strtemp = strtemp + line
            if not line:
                if strtemp != '':
                    ws.write(li + i, row + 1, strtemp)
                    strtemp = ''
                break
        wb2.save('testdemo3.xls')


# read_file()
# write_excelfile3()
# write_excelfile2()
write_excelfile()
